/*
 * CMediaManager.cpp
 *
 *  Created on: Sep 6, 2011
 *      Author: welcome
 */

#include "CMediaManager.h"
#include <stdio.h>
#include <string>
#include <map>

namespace GGEngine {

CMediaManager::CMediaManager() {

}

CMediaManager::~CMediaManager() {
}

/* Mapping the file extension to the media type */
typedef std::pair<std::string, int> kv;
static kv kvTable[] = {
	kv("wav", MEDIA_SOUND_WAV),
	kv("mid", MEDIA_SOUND_MIDI),
	kv("mp3", MEDIA_SOUND_MP3),
	kv("ogg", MEDIA_SOUND_OGG)
};
static std::map<std::string, int> MediaType(kvTable, kvTable + 4);

/* Returns file extension */
static std::string getExtName(std::string path)
{
	return path.substr(path.find_last_of('.') + 1);
}

/* Guess the media type based on file extension */
static int guessMediaType(std::string path)
{
	std::string ext = getExtName(path);
	std::map<std::string, int>::iterator it = MediaType.find(ext);
	if(it == MediaType.end())
	{
		kdLogMessage("matting error");
		UTIL_ASSERT(false);
	}
	return it->second;
}

CMediaSound* CMediaManager::getSound(const char* location)
{
	return CMediaManager::getSound(location, guessMediaType(location));
}

CMediaSound* CMediaManager::getSound(const char* location, int mediaType)
{
	UTIL_ASSERT(location != NULL);

	CMediaSound* ms = NULL;
	ms = new CMediaSound();
	ms->setMediaType(mediaType);
	ms->setLocation(location);
	return ms;
}
} /* namespace GGEngine */
